package com.feini.ktv.svc.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class SysTimeUtil {
    private static final ConcurrentMap<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap();
    private static final int PATTERN_CACHE_SIZE = 50;

    public static String getSysDateDef(String date) {
        return format(LocalDateTime.now(), date);
    }

    public static String getSysDate() {
        return format(LocalDateTime.now(), "yyyy-MM-dd");
    }

    public static String getSysDates() {
        return format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getYYYYMM() {
        return format(LocalDateTime.now(), "yyyyMM");
    }

    public static String getSysDateYYYYMMDDHHMMSS() {
        return format(LocalDateTime.now(), "yyyyMMddHHmmss");
    }

    public static Date addMonths(Date date, int amount) {
        return add(date, 2, amount);
    }

    public static Date string2Date(String strDate, String format) throws Exception {
        if (null == strDate) {
            throw new NullPointerException();
        } else {
            DateFormat df = new SimpleDateFormat(format);
            return df.parse(strDate);
        }
    }


    private static String format(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = createCacheFormatter(pattern);
        return localDateTime.format(formatter);
    }

    private static DateTimeFormatter createCacheFormatter(String pattern) {
        if (pattern != null && pattern.length() != 0) {
            DateTimeFormatter formatter = FORMATTER_CACHE.get(pattern);
            if (formatter == null && FORMATTER_CACHE.size() < 50) {
                formatter = DateTimeFormatter.ofPattern(pattern);
                DateTimeFormatter oldFormatter = FORMATTER_CACHE.putIfAbsent(pattern, formatter);
                if (oldFormatter != null) {
                    formatter = oldFormatter;
                }
            }

            return formatter;
        } else {
            throw new IllegalArgumentException("Invalid pattern specification");
        }
    }

    private static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(calendarField, amount);
            return c.getTime();
        }
    }

    private static Date getMonthEndTime(Calendar cal) {

        Calendar tmpCalendar = (Calendar) cal.clone();
        tmpCalendar.set(Calendar.DAY_OF_MONTH, 1);
        tmpCalendar.set(Calendar.HOUR_OF_DAY, 0);
        tmpCalendar.set(Calendar.MINUTE, 0);
        tmpCalendar.set(Calendar.SECOND, 0);
        tmpCalendar.set(Calendar.MILLISECOND, 0);
        tmpCalendar.add(Calendar.MONTH, 1);
        tmpCalendar.add(Calendar.MILLISECOND, -1);// 往前移动1毫秒,即调到上个月最后一天
        return tmpCalendar.getTime();
    }

    private static Date getMonthEndTime(Date date) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getMonthEndTime(cal);
    }

    public static Date getDateByPattern(String time, String pattern) throws Exception {
        Date rtn = null;
        DateFormat dateformat = new SimpleDateFormat(pattern);
        rtn = dateformat.parse(time.trim());
        return rtn;
    }

}
